package com.ruoyi.web.controller.module.estate;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.estate.PaySlip;
import com.ruoyi.system.domain.module.excel.PropertyWyfTemplateExcel;
import com.ruoyi.system.service.module.estate.IPaySlipService;
import com.ruoyi.system.service.module.wx.IWxUserService;
import com.ruoyi.system.service.system.ISysUserService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

/**
 * 工资条Controller
 *
 * @author ruoyi
 * @date 2025-03-24
 */
@Validated
@Api(value = "工资条控制器", tags = {"工资条管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/paySlip")
public class PaySlipController extends BaseController {

    private final IPaySlipService paySlipService;
	private final IWxUserService wxUserService;
	private final ISysUserService sysUserService;
    /**
     * 查询工资条列表
     */
    @ApiOperation("查询工资条列表")
    @PreAuthorize("@ss.hasPermi('module:paySlip:list')")
    @GetMapping("/list")
    public TableDataInfo<PaySlip> list(@Validated PaySlip paySlip) {
        QueryWrapper<PaySlip> qw = new QueryWrapper<>();
		if (paySlip.getPhoneNum()!=null){
			qw.eq("phone_num",paySlip.getPhoneNum());
		}
		if (paySlip.getRealName()!=null){
			qw.like("real_name",paySlip.getRealName());
		}
        TableDataInfo<PaySlip> td = PageUtils.buildDataInfo(paySlipService.page(PageUtils.buildPage(),qw));
        List<PaySlip> list = td.getRows();
		if (list.size()>0){
			for (PaySlip slip:list){
				if (slip!=null){
					if (slip.getWxUserId()!=null){
						slip.setWxUserName(wxUserService.findById(slip.getWxUserId()).getNickName());
					}else {
						slip.setWxUserName("暂未签字");
					}
					if (slip.getSysUserId()!=null){
						slip.setSysUserName(sysUserService.selectUserById(Long.valueOf(slip.getSysUserId())).getNickName());
					}else {
						slip.setSysUserName("暂未签字");
					}
				}
			}
		}
        return td;
    }

	@Log(title = "物业费导入", businessType = BusinessType.IMPORT)
	@PreAuthorize("@ss.hasAnyPermi('module:paySlip:import')")
	@PostMapping("/importData")
	public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
		ExcelUtil<PaySlip> util = new ExcelUtil<>(PaySlip.class);
		List<PaySlip> templateExcels = util.importExcel(file.getInputStream());
		SysUser sysUser = getUser();
		String operName = sysUser.getUserName();
		String message = paySlipService.importWyfData(templateExcels,updateSupport,operName);
		return AjaxResult.success(message);
	}


    /**
     * 导出工资条列表
     */
    @ApiOperation("导出工资条列表")
    @PreAuthorize("@ss.hasPermi('module:paySlip:export')")
    @Log(title = "工资条", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<PaySlip> export(@Validated PaySlip paySlip) {
        List<PaySlip> list = paySlipService.list();
        ExcelUtil<PaySlip> util = new ExcelUtil<PaySlip>(PaySlip.class);
        return util.exportExcel(list, "工资条");
    }

    /**
     * 获取工资条详细信息
     */
    @ApiOperation("获取工资条详细信息")
    @PreAuthorize("@ss.hasPermi('module:paySlip:query')")
    @GetMapping("/{id}")
    public AjaxResult<PaySlip> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        PaySlip paySlip = paySlipService.getById(id);
        return AjaxResult.success(paySlip);
    }

    /**
     * 新增工资条
     */
    @ApiOperation("新增工资条")
    @PreAuthorize("@ss.hasPermi('module:paySlip:add')")
    @Log(title = "工资条", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody PaySlip paySlip) {
        return toAjax(paySlipService.save(paySlip) ? 1 : 0);
    }

    /**
     * 修改工资条
     */
    @ApiOperation("修改工资条")
    @PreAuthorize("@ss.hasPermi('module:paySlip:edit')")
    @Log(title = "工资条", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody PaySlip paySlip) {
        return toAjax(paySlipService.updateById(paySlip) ? 1 : 0);
    }

    /**
     * 删除工资条
     */
    @ApiOperation("删除工资条")
    @PreAuthorize("@ss.hasPermi('module:paySlip:remove')")
    @Log(title = "工资条" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(paySlipService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
